package com.zm.devmag.device.service.Impl;

import java.io.Console;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zm.devmag.device.Vo.DeviceDetaiVo;
import com.zm.devmag.device.dao.DeviceDetailMapper;
import com.zm.devmag.device.service.DeviceStatisticsServer;

@Service
public class DeviceStatisticsServerImpl implements DeviceStatisticsServer {
	@Autowired
	private DeviceDetailMapper ddMapper;
	@Override
	public Object searchDeviceStatistics() {
		List<DeviceDetaiVo> oridata = ddMapper.selectAllNoConf();
		List<Map<String,Object>> returndata= new ArrayList<Map<String,Object>>();
		
		for(DeviceDetaiVo dv:oridata) {
			if(returndata.size()<=0) {
				Map<String,Object> fmd = new HashMap<String,Object>();
				fmd.put("wid",dv.getWid());
				fmd.put("whname",dv.getWhname());
				returndata.add(fmd);
			}else {
				Boolean isin =false;
				for(Map<String,Object>tmp :returndata) {
					if(tmp.get("wid").equals(dv.getWid())) {
						isin = true;
						break;
					}
				}
				if(!isin) {
					Map<String,Object> fmd = new HashMap<String,Object>();
					fmd.put("wid",dv.getWid());
					fmd.put("whname",dv.getWhname());
					returndata.add(fmd);
				}else {
					continue;
				}
			}
		}
		
		for(DeviceDetaiVo dv:oridata) {
			Map<String,Object> tmpfor= new HashMap<>() ;
			for(Map<String,Object>tmp :returndata) {
				if(tmp.get("wid").equals(dv.getWid())) {
					tmpfor = tmp;
					break;
				}
			}
			if(!tmpfor.containsKey("cdls")) {
				tmpfor.put("dcidstr",dv.getDcid().toString());
				tmpfor.put("cdcnt",1);
				tmpfor.put("ins", 0);
				tmpfor.put("outs", 0);
				tmpfor.put("diss",0);
				switch(dv.getStatus()) {
					case "1" :
						tmpfor.put("ins", 1);
					break;
					case "2" :
						tmpfor.put("outs", 1);
					break;
					case "3" :
						tmpfor.put("diss", 1);
					break;
				}
				String tepstr = (String)tmpfor.get("dcidstr");
				String[] dcidarr=tepstr.split(";") ;
				tmpfor.put("cdls",dcidarr.length);
			}else {
				String tepstr = (String)tmpfor.get("dcidstr");
				if(!tepstr.contains(dv.getDcid().toString())) {
					tmpfor.put("dcidstr", tmpfor.get("dcidstr")+";"+dv.getDcid());
					String tepstr1 = (String)tmpfor.get("dcidstr");
					String[] dcidarr1=tepstr1.split(";") ;
					tmpfor.put("cdls", dcidarr1.length);	
				}
				tmpfor.put("cdcnt",(Integer)tmpfor.get("cdcnt")+1);
				switch(dv.getStatus()) {
					case "1" :
						tmpfor.put("ins", (Integer)tmpfor.get("ins")+1);
					break;
					case "2" :
						tmpfor.put("outs", (Integer)tmpfor.get("outs")+1);
					break;
					case "3" :
						tmpfor.put("diss", (Integer)tmpfor.get("diss")+1);
					break;
				}
			}
		}
		return returndata;
	}
	 public Object searchStatisticsWidsDD(Long wid) {
		 List<Map<String,Object>> returndatalist= new ArrayList<Map<String,Object>>();
		 
		 Map<String,Object> cond = new HashMap<String,Object>();
		 cond.put("wid", wid);
		 List<DeviceDetaiVo> ddlist = ddMapper.selectAllNoLimit(cond);
		 Map<String,Object> returndata = new HashMap<String,Object>();
		 for(DeviceDetaiVo dv:ddlist) {
			 //统计分类
			 returndata.put("whname", dv.getWhname());
			 if(returndatalist.size()<=0) {
					Map<String,Object> fmd = new HashMap<String,Object>();
					fmd.put("dcid",dv.getDcid());
					fmd.put("dcname",dv.getCdname());
					returndatalist.add(fmd);
				}else {
					Boolean isin =false;
					for(Map<String,Object>tmp :returndatalist) {
						if(tmp.get("dcid").equals(dv.getDcid())) {
							isin = true;
							break;
						}
					}
					if(!isin) {
						Map<String,Object> fmd = new HashMap<String,Object>();
						fmd.put("dcid",dv.getDcid());
						fmd.put("dcname",dv.getCdname());
						returndatalist.add(fmd);
					}else {
						continue;
					}
				}
		 }
		 for(DeviceDetaiVo dv:ddlist) {
				Map<String,Object> tmpfor= new HashMap<>() ;
				for(Map<String,Object>tmp :returndatalist) {
					if(tmp.get("dcid").equals(dv.getDcid())) {
						tmpfor = tmp;
						break;
					}
				}
				if(!tmpfor.containsKey("ins")) {
					tmpfor.put("ins", 0);
					tmpfor.put("outs", 0);
					tmpfor.put("diss",0);
					switch(dv.getStatus()) {
						case "1" :
							tmpfor.put("ins", 1);
						break;
						case "2" :
							tmpfor.put("outs", 1);
						break;
						case "3" :
							tmpfor.put("diss", 1);
						break;
					}
				}else {
					switch(dv.getStatus()) {
						case "1" :
							tmpfor.put("ins", (Integer)tmpfor.get("ins")+1);
						break;
						case "2" :
							tmpfor.put("outs", (Integer)tmpfor.get("outs")+1);
						break;
						case "3" :
							tmpfor.put("diss", (Integer)tmpfor.get("diss")+1);
						break;
					}
				}
			}
		 List<String> cdlist = new ArrayList<String>();//纵坐标
		 List<Map<String,Object>> series = new ArrayList<Map<String,Object>>();
		 Map<String,Object> tdata1 = new HashMap<String,Object>();;
		 tdata1.put("type", "column");
		 tdata1.put("name", "入库数");
		 List<Integer> tldata1 = new ArrayList<Integer>();;
		 for(Map<String,Object>aa :returndatalist) {
			 cdlist.add((String)aa.get("dcname"));
			 tldata1.add((Integer)aa.get("ins"));
		 }
		 tdata1.put("data", tldata1);
		 series.add(tdata1);
		 
		 Map<String,Object> tdata2 = new HashMap<String,Object>();
		 tdata2.put("type", "column");
		 tdata2.put("name", "出库数");
		 List<Integer> tldata2 = new ArrayList<Integer>();;
		 for(Map<String,Object>aa :returndatalist) {
			 tldata2.add((Integer)aa.get("outs"));
		 }
		 tdata2.put("data", tldata2);
		 series.add(tdata2);
		 
		 Map<String,Object> tdata3 = new HashMap<String,Object>();;
		 tdata3.put("type", "column");
		 tdata3.put("name", "报废数");
		 List<Integer> tldata3 = new ArrayList<Integer>();;
		 for(Map<String,Object>aa :returndatalist) {
			 tldata3.add((Integer)aa.get("diss"));
		 }
		 tdata3.put("data", tldata3);
		 series.add(tdata3);
		 
		 returndata.put("datalist", returndatalist);
		 returndata.put("series", series);
		 returndata.put("cdlist", cdlist);
		 return returndata;
	 }
	 @SuppressWarnings("unchecked")
	public Object searchStatisticsDcidDatail(Map<String, Object>cond) {
		 String status= (String)cond.get("status");
		 String str="";
		 switch(status) {
		    case "1":
		    	str="入库数";
		    	break;
		    case "2":
		    	str="出库数";
		    	break;
		    case "3":
		    	str="报废数";
		    	break;
		 }
		 int listlen = ddMapper.selectStaticAllCount(cond);
		 Map<String,Object> returndata = new HashMap<String,Object>();
		 List<DeviceDetaiVo> ddlist=ddMapper.selectStaticAll(cond);
		 List<DeviceDetaiVo> ddlist2= ddMapper.selectStaticAllNoLimit(cond);
		 List<String> xaix=new ArrayList<String>();
		 Map<String,Object> series = new HashMap<String,Object>();
		 series.put("values", new ArrayList<Integer>());
		 series.put("text", str);
		 for(DeviceDetaiVo dv:ddlist2) {
			 if(xaix.contains(dv.getTcrq())) {
				 List<Integer> tems = (List<Integer>)series.get("values");
				 tems.set(xaix.indexOf(dv.getTcrq()), tems.get(xaix.indexOf(dv.getTcrq()))+1);
				 
			 }else {
				 xaix.add(dv.getTcrq());
				 ((List<Integer>)series.get("values")).add(1);
			 }
		 }
		 
		 returndata.put("listlen", listlen);
		 returndata.put("ddlist", ddlist);
		 returndata.put("xaix", xaix);
		 returndata.put("series", series);
		 return returndata;
	 }
}

